class AccomodationsController < ApplicationController
  # GET /accomodations
  # GET /accomodations.xml
  
  def index
    @accomodations = Accomodation.all
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @accomodations }
    end
  end
  
  # GET /accomodations/1
  # GET /accomodations/1.xml
  def show
    @accomodation = Accomodation.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @accomodation }
    end
  end
  
  # GET /accomodations/new
  # GET /accomodations/new.xml
  def new
    @accomodation = Accomodation.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @accomodation }
    end
  end
  
  # GET /accomodations/1/edit
  def edit
    @accomodation = Accomodation.find(params[:id])
  end
  
  # POST /accomodations
  # POST /accomodations.xml
  def create
    @accomodation = Accomodation.new(params[:accomodation])
    @accomodation.checkedOut = false
    respond_to do |format|
      if @accomodation.save
        format.html { redirect_to(@accomodation, :notice => 'Accomodation was successfully created.') }
        format.xml  { render :xml => @accomodation, :status => :created, :location => @accomodation }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @accomodation.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /accomodations/1
  # PUT /accomodations/1.xml
  def update
    @accomodation = Accomodation.find(params[:id])
    
    respond_to do |format|
      if @accomodation.update_attributes(params[:accomodation])
        format.html { redirect_to(@accomodation, :notice => 'Accomodation was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @accomodation.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /accomodations/1
  # DELETE /accomodations/1.xml
  def destroy
    @accomodation = Accomodation.find(params[:id])
    @accomodation.destroy
    
    respond_to do |format|
      format.html { redirect_to(accomodations_url) }
      format.xml  { head :ok }
    end
  end
  
  def select_room
    
  end
  
  def show_bill
    @room = Room.find_by_num(params[:num])
    if (@room.nil?)      
    else
      @acc = Accomodation.find_by_room_id(params[:num])
      if(@acc.blank?)
      respond_to do |format|
        flash[:notice] = 'There is no accomodation on that room.'      
        format.html { redirect_to(:action => 'index' ) }
       end
      else
      @requests = Request.find(:all, :conditions => ['accomodation_id=?', @acc.id])
      @services = Service.new
      @roomdesc = Roomdesc.new
      @roomdesc = Roomdesc.find_by_id(@room.roomtype_id)
      @count = 1;
      @services_cost = 0;
      @daily_price = @roomdesc.dailyPrice;
      @num_days = @acc.exit_date - @acc.arrive_date
      cookies[:acc_id] = {:value => @acc.id, :expires => 31.days.from_now }
      end
    end    
  end
  
  def checkout
    @cookie_data = cookies[:acc_id]
    cookies.delete :acc_id
    @acc = Accomodation.find_by_id(@cookie_data)    
    respond_to do |format|
     if @acc.nil?        
        flash[:notice] = 'There was a problem checking out. Invalid accomodation.'      
        format.html { redirect_to(:action => 'show_bill' ) }       
        format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
      else
        @acc.checkedOut = true
        @acc.update_attribute(:checkedOut, true) 
        flash[:notice] = 'Payment succeeded! Accomodation checked out. Thanks and come again!'
        @request = Request.new
        @request = Request.find(:all, :conditions => ['accomodation_id = ?', @acc.id])
        @request.each do |r|
          r.destroy
        end
        @acc.destroy
        format.html { redirect_to(:action => 'show_bill' ) }     
    end 
   end
 end
  
  
end
